import axios from "axios";

/**
 *
 * @param {*} info 一些响应式对象
 * @param {*} route 路由函数
 */
export default function remoteData(info) {
  /**
   * 请求最近文章给list，总数给count
   * @param {*} page 分页
   * @param {*} list 响应式
   * @param {*} count 响应式
   * @returns Promise resolve(data) reject(response)
   */
  const get_articles = (page, list, count) => {
    return getArticles(info, page, list, count);
  };
  /**
   * 请求最近动态给list，总数给count
   * @param {*} page 分页
   * @param {*} list 响应式
   * @param {*} count 响应式
   * @returns Promise resolve(data) reject(response)
   */
  const get_essays = (page, list, count) => {
    return getEssays(info, page, list, count);
  };

  const get_collecions = (page, list, count) => {
    return getCollections(info, page, list, count);
  };
  /**
   * get essay by badge_id
   */
  const get_essays_with_badge = (page, list, count, id) => {
    return getContentWithWhat(info, "essay", "badges", page, list, count, id);
  };
  /**
   * get articles by badge_id
   */
  const get_articles_with_badge = (page, list, count, id) => {
    return getContentWithWhat(info, "article", "badges", page, list, count, id);
  };
  /**
   * get essays by collection_id
   */
  const get_essays_with_collection = (page, list, count, id) => {
    return getContentWithWhat(
      info,
      "essay",
      "collections",
      page,
      list,
      count,
      id
    );
  };
  /**
   * get articles by collecion_id
   */
  const get_articles_with_collection = (page, list, count, id) => {
    return getContentWithWhat(
      info,
      "article",
      "collections",
      page,
      list,
      count,
      id
    );
  };

  /**
   * 获得文章详细具体信息
   * @param {*} content
   * @param {*} id
   * @returns
   */
  const get_article_details = (content, id) => {
    return getContentDetails(info, content, "articles", id);
  };
  /**
   * 获得动态详细具体信息
   * @param {*} content
   * @param {*} id
   * @returns
   */
  const get_essay_details = (content, id) => {
    return getContentDetails(info, content, "essays", id);
  };
  return {
    get_articles,
    get_essays,
    get_collecions,
    get_essays_with_badge,
    get_articles_with_badge,
    get_articles_with_collection,
    get_essays_with_collection,
    get_article_details,
    get_essay_details,
  };
}

const getContentDetails = (info, content, type, id) => {
  return new Promise((resolve, reject) => {
    axios
      .get(`/api/${type}/${id}/`)
      .then((response) => {
        content.value = response.data;
        resolve(response.data);
      })
      .catch((error) => {
        reject(error.response);
      })
      .then();
  });
};

const getCollections = (info, page, list, count) => {
  return new Promise((resolve, reject) => {
    axios
      .get(`/api/collections/?page=${page}`)
      .then((response) => {
        list.value = response.data.results;
        count.value = response.data.count;
        resolve(response.data);
      })
      .catch((error) => {
        reject(error.response);
      })
      .then();
  });
};

const getArticles = (info, page, list, count) => {
  return new Promise((resolve, reject) => {
    axios
      .get(`/api/articles/?page=${page}`)
      .then(function (response) {
        list.value = response.data.results;
        count.value = response.data.count;
        resolve(response.data);
      })
      .catch(function (error) {
        reject(error.response);
      })
      .then(function () {
        // 总是会执行
      });
  });
};

const getEssays = (info, page, list, count) => {
  return new Promise((resolve, reject) => {
    axios
      .get(`/api/essays/?page=${page}`)
      .then(function (response) {
        list.value = response.data.results;
        count.value = response.data.count;
        resolve(response.data);
      })
      .catch(function (error) {
        reject(error.response);
      })
      .then(function () {
        // 总是会执行
      });
  });
};

/**
 *
 * @param {*} info
 * @param {*} action 具体content内容(article,essay)
 * @param {*} what  搜寻的索引类型(badges,collections)
 * @param {*} page 页码
 * @param {*} list 响应式
 * @param {*} count 响应式
 * @param {*} id 索引的id
 * @returns
 */
const getContentWithWhat = (info, action, what, page, list, count, id) => {
  return new Promise((resolve, reject) => {
    axios
      .get(`/api/${what}/${id}/get_content/`, {
        params: {
          type: action,
          page: page,
        },
      })
      .then(function (response) {
        list.value = response.data.results;
        count.value = response.data.count;
        resolve(response.data);
      })
      .catch(function (error) {
        reject(error.response);
      })
      .then(function () {
        // 总是会执行
      });
  });
};
